#!/usr/bin/env python
import io
import os
import sys
import time
import datetime
import getopt
import numpy 
import scipy
import scipy.linalg as sla
import scipy.sparse
from scipy.linalg import eigh
from scipy.sparse.linalg import eigsh

map_keys = {}

class Parser() :
	path = 'output/'
	datadir = '../data/'
	#output = open( path +  'output7.txt', 'a')
	
	def __init__(self, fname) :
		print ('parser inited')
		
		#self.output.write(str(datetime.datetime.now(None)) + '\n')

	def parse(self, filename) :
		print ("parsing: " + filename)
		_file = open (self.datadir +  filename, 'r')
		
		#self.output.write('\n' + filename + '\n')
		global nnz
		nnz = 0
		
		f = _file.readlines()
		_file.close()
		for line in f :
			nnz += 1
			values = line.split(' ')
			if map_keys.get(values[0]) == None:
				map_keys[values[0]] = len(map_keys)
			if map_keys.get(values[1]) == None:
				map_keys[values[1]] = len(map_keys)
		
		n = len(map_keys)
		matrix = numpy.zeros((n,n), dtype=numpy.float)
		print ("matrix size: " + str(n))
		
		sys.exit(-1)
		
		for line in f :
			values = line.split('\t')
			row_idx = map_keys.get(values[0])
			col_idx = map_keys.get(values[1])
			matrix[row_idx][col_idx] = float(str.strip(values[2]))
		
		print ("running program")
		print ('stats')
		print ('#el: ' + str(matrix.size))
		print ('dimen: ' + str(matrix.ndim))
		print ('nnz: ' + str(nnz) + '\t')
		density = float( nnz) / float(18600* (18600-1))
		#self.output.write (str(density) + '\t')
		print ('density: ' + str(density))
		#print ('issparse: ' + str(scipy.sparse.isspmatrix(matrix)) )
		
		#Determinant
		#det =  float(scipy.linalg.det(matrix))
		#self.output.write('Determinant: ' +  str(det) + '\n')
		#print ('det: ' + str(det) )
		
		#evals_large, evecs_large = eigsh(matrix, 3, which='LM')
		#self.output.write('Large eigen vals\n')
		#numpy.savetxt(self.output, evals_large)
		
		#self.output.write('Large eigen vecs\n')		
		#numpy.savetxt(self.output, evecs_large)
		#self.output.write('\n')
		#self.output.flush();
		#
		#evals_small, evecs_small = eigsh(matrix, 3, which='SM', tol=1E-2)
		#self.output.write('Small eigen vals\n')
		#numpy.savetxt(self.output, evals_small)
		#self.output.write('\n')
		#self.output.flush();
		#self.output.write('Small eigen vecs\n')
		#numpy.savetxt(self.output,evecs_small)
		#self.output.write('\n')
		
		#Norms
		self.output.write('n1\tinf\tfro\n')
		n1 = sla.norm(matrix, 1)
		self.output.write(str(n1) + '\t')
		print ("calculated n1")
		#n2 = sla.norm(matrix, 2)
		_inf = sla.norm(matrix, float('inf'))
		print ("calculated inf")
		self.output.write(str(_inf) + '\t')
		fro = sla.norm(matrix, 'fro')
		print ("calculated fro")
		self.output.write(str(fro) + '\n')
		
		#print ('svd' + str(scipy.lialg.svd(matrix)))
		#print ('svd' + str(scipy.lialg.diagsvd(matrix)))
		
		#sim_matrix = open (path + 'sim_matrix.out','w')
		#numpy.savetxt(path + 'sim_matrix.out', matrix);
		
		#protein_names = open( path + 'protein_names.out', 'w')
		#protein_names.write(str(map_keys.items()))
		self.output.flush();

def main() :
	_filename = 'ite-$.ite_'
	args = sys.argv[1:]
	
	_Parser = Parser(None)
	_Parser.parse(args[0])
	#for i in range(7, 8) :
	#	_Parser.parse(_filename.replace('$', str(i)))
	#if len(args) > 0 :
	#	Parser(args[0])
	_Parser.output.close()

if __name__ == '__main__':
	main()